package com.alotus.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alotus.model.User;
import com.alotus.service.UserService;
import com.alotus.util.common.BaseController;
import com.alotus.util.common.Const;
import com.alotus.view.Json;

/**
 * 测试测试
 * @author silence.cheng
 * @date 2016-06-07 23:36
 * 访问url:http://localhost:8080/alotus/user/showUser
 */

@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User>{
	@Resource
	private UserService userService;
	
	@Autowired
	private HttpServletRequest request;
	
	private static final Logger logger = Logger
			.getLogger(UserController.class);
	
	@RequestMapping("/showUser")
	public String toIndex(HttpServletRequest request,Model model){
		User user = this.userService.selectByPrimaryKey(1);
		model.addAttribute("user", user);
		return "showUser";
	}
	/**
	 * 测试json输出
	 * http://localhost:8080/alotus/user/getUser.json?id=1
	 */
	@ResponseBody
	@RequestMapping("/getUser")
	public Map getUser(@RequestParam("id") String id) {
		HashMap<String, Object> jsonMap=new HashMap<String, Object>();
		if (StringUtils.isBlank(request.getParameter("id"))) {
			jsonMap=pakeageJson(id,Const.NO_PARAMATER);
		}
		try{
			User user = this.userService.selectByPrimaryKey(Integer.parseInt(id));
			jsonMap=pakeageJson(user,null);
		}catch(Exception e){
			jsonMap=pakeageJson(null,e.getMessage());
			logger.info("getUser error"+e.getMessage());
		}
			
		return jsonMap;
	
	}
}